//
//  QYHoldingListBookViewController.m
//  QingYue
//
//  Created by Zheng on 2017/7/24.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYHoldingListBookViewController.h"
#import "QYBookShelfCollectionViewCell.h"
#import "QYHotBookListModel.h"
#import "QYBookDetailViewController.h"

#define QYBookShelfCollectionViewCellID @"QYBookShelfCollectionViewCell"
#define QYBookShelfHeaderViewID @"QYBookShelfHeaderView"
static NSString *const MultilevelHeaderCellID = @"DYMultilevelHeaderView";

@interface QYHoldingListBookViewController () <UICollectionViewDelegate,UICollectionViewDataSource>

/** 列表collectionView*/
@property (nonatomic,strong) UICollectionView* listCollectionView;

@property (nonatomic, strong) NSMutableArray<QYHotBookListModel *> *dataArr;



@end

@implementation QYHoldingListBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [self.view addSubview:self.listCollectionView];
}

- (void)setHoldingType:(NSString *)holdingType {
    _holdingType = holdingType;
    
    [self loadData];
}

- (void)loadData {
    
    _dataArr = [NSMutableArray new];
    
    [[YCRequestDataService sharedManager] Post:@"GetBooksByHoldingType" parameters:@{@"holdingType":_holdingType} success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            
            [SVProgressHUD dismissWithDelay:.35];
            
            [[response allKeys] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if(![obj isEqualToString:@"Code"] && ![obj isEqualToString:@"Exception"]) {
                    NSArray<NSDictionary *> *dataList = response[obj];
                    [dataList enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        QYHotBookListModel *model = [[QYHotBookListModel alloc] initWithDataDic:obj];
                        [_dataArr addObject:model];
                    }];
                }
            }];
            
            [_listCollectionView reloadData];
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}

#pragma mark - 布局
- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [_listCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
}

#pragma mark - 滑动的时候
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_listCollectionView endEditing:YES];
}


#pragma mark - collectionView delegate and datasource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    QYBookShelfCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:QYBookShelfCollectionViewCellID forIndexPath:indexPath];
    
    if (_dataArr.count != 0) {
        QYHotBookListModel *model = _dataArr[indexPath.row];
        cell.model = model;
    }
    
    
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    QYHotBookListModel *model = _dataArr[indexPath.row];
    
    QYBookDetailViewController *bookDetailVC = [[QYBookDetailViewController alloc] init];
    bookDetailVC.hidesBottomBarWhenPushed = YES;
    bookDetailVC.isbn = model.Isbn;
    [self.navigationController pushViewController:bookDetailVC animated:YES];
    
}

#pragma mark - lazy load
- (UICollectionView *)listCollectionView
{
    if (!_listCollectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumLineSpacing = 1;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.itemSize = CGSizeMake((MSSize_Width) / 3, 230);
        _listCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _listCollectionView.backgroundColor = RGB(241, 242, 242, 1.0);
        _listCollectionView.delegate = self;
        _listCollectionView.dataSource = self;
        [_listCollectionView registerClass:[QYBookShelfCollectionViewCell class] forCellWithReuseIdentifier:QYBookShelfCollectionViewCellID];
    }
    return _listCollectionView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
